from collections import Counter
import numpy as np
import matplotlib.pyplot as plt


def plot_strength(strengths, save_name):

    # 使用Counter来统计每个值出现的次数
    strength_distribution = Counter(strengths)

    sorted_strength_distribution = sorted(strength_distribution.items(), key=lambda item: item[0], reverse=True)

    # 计算直方图的bin边界和每个bin中的计数
    bins = 10  # 定义bin的数量
    hist, bin_edges = np.histogram(strengths, bins=bins)

    # 计算每个bin的中心点，用于绘图
    bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])

    # 绘制条形图
    plt.figure(figsize=(12, 8))
    bars = plt.bar(bin_centers, hist, width=(bin_edges[1] - bin_edges[0]), alpha=0.7, color='blue', edgecolor='black')

    # 在每个条形上方添加文本
    for bar, count in zip(bars, hist):
        height = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2, height, str(count), ha='center', va='bottom')

    # 添加标题和标签
    plt.title('Distribution of Preference Strength in Bins')
    plt.xlabel('Preference Strength Bins')
    plt.ylabel('Count')


    plt.savefig(save_name)


